﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Threading;

namespace Matopeli
{
    enum Suunta
    {
        Left,
        Right,
        Up,
        Down,
    }

    public partial class MainWindow : Window
    {
        DispatcherTimer timer = new DispatcherTimer();

        Peli peli = new Peli();
        bool gameOver = false;

        Suunta suunta = Suunta.Left;

        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = peli;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            peli.Matonen.X = canvas.Width / 2D;
            peli.Matonen.Y = canvas.Height / 2D;
            peli.Matonen.Size = 10D;

            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = TimeSpan.FromMilliseconds(10);
            timer.IsEnabled = true;
        }

        void timer_Tick(object sender, EventArgs e)
        {
            Ellipse ellipse;

            // siirrä matonen

            double step = 2.5, stepX = 0, stepY = 0;
            switch (suunta)
            {
                case Suunta.Up:
                    stepX = 0; stepY = -step;
                    break;
                case Suunta.Down:
                    stepX = 0; stepY = step;
                    break;
                case Suunta.Left:
                    stepX = -step; stepY = 0;
                    break;
                case Suunta.Right:
                    stepX = step; stepY = 0;
                    break;
            }
            if (!peli.Matonen.Move(stepX, stepY))
            {
                gameOver = true;
                var tb = new TextBlock();
                tb.Text = "Game Over";
                Canvas.SetLeft(tb, 50);
                Canvas.SetTop(tb, 50);
                canvas.Children.Add(tb);
            }

            // piirrä omput... syö samalla

            canvas.Children.Clear();

            var syodyt = new List<Omena>();

            foreach (var omena in peli.Omenat)
            {
                if (peli.Matonen.Eat(omena))
                {
                    omena.Syoty = true;
                    syodyt.Add(omena);
                    continue;
                }

                ellipse = new Ellipse();
                ellipse.Width = omena.Size;
                ellipse.Height = omena.Size;
                ellipse.Fill = omena.Syoty ? Brushes.Transparent : Brushes.Red;
                
                Canvas.SetLeft(ellipse, omena.ActualX);
                Canvas.SetTop(ellipse, omena.ActualY);

                canvas.Children.Add(ellipse);
            }

            // poistetaan syödyt omput

            foreach (var omena in syodyt)
                peli.Omenat.Remove(omena);

            // piirrä häntä

            foreach (var pos in peli.Matonen.Tail)
            {
                ellipse = new Ellipse();
                ellipse.Width = peli.Matonen.Size;
                ellipse.Height = peli.Matonen.Size;
                ellipse.Fill = Brushes.DarkGreen;

                Canvas.SetLeft(ellipse, pos.X - peli.Matonen.Size / 2);
                Canvas.SetTop(ellipse, pos.Y - peli.Matonen.Size / 2);

                canvas.Children.Add(ellipse);
            }

            // piirrä matonen

            ellipse = new Ellipse();
            ellipse.Width = peli.Matonen.Size;
            ellipse.Height = peli.Matonen.Size;
            ellipse.Fill = Brushes.DarkGreen;

            Canvas.SetLeft(ellipse, peli.Matonen.ActualX);
            Canvas.SetTop(ellipse, peli.Matonen.ActualY);

            canvas.Children.Add(ellipse);
        }

        private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var pos = e.GetPosition((IInputElement)sender);

            peli.Omenat.Add(new Omena(pos.X, pos.Y));
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Up:
                    suunta = Suunta.Up;
                    break;
                case Key.Down: 
                    suunta = Suunta.Down;
                    break;
                case Key.Left: 
                    suunta = Suunta.Left;
                    break;
                case Key.Right: 
                    suunta = Suunta.Right;
                    break;
            }
        }
    }
}
